package com.google.precinct.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

public class BreadCrumbManager {

	ArrayList<Runnable> onReturn = new ArrayList<Runnable>();
	ArrayList<Runnable> onRevoke = new ArrayList<Runnable>();
	private final FlowPanel breadcrumbPanel;

	public BreadCrumbManager(FlowPanel breadcrumbPanel) {
		this.breadcrumbPanel = breadcrumbPanel;
	}

	public void pushEntry(String text, Runnable onReturnRun, Runnable onRevokeRun) {
		if (this.onReturn.size() > 0) {
			// Convert previous entry to a link
			String prevtext = ((InlineLabel) breadcrumbPanel.getWidget(
					breadcrumbPanel.getWidgetCount() - 1)).getText();
			breadcrumbPanel.remove(breadcrumbPanel.getWidgetCount() - 1);
			breadcrumbPanel.add(makeCrumbLink(prevtext, onReturn.size() - 1));
			breadcrumbPanel.add(getGTSeparator());
		}
		// Put in the current crumb
		breadcrumbPanel.add(makeCurrentCrumb(text));
		onReturn.add(onReturnRun);
		onRevoke.add(onRevokeRun);
	}

//	public void pushEntry(String text, Runnable onReturn, Runnable onRevoke, String previous) {
//
//	}

	public void popEntry() {
		popEntryNoRun();
		this.onReturn.get(this.onRevoke.size() - 1).run();
	}

	public void popEntryNoRun() {
		// Pop 1 level of the breadcrumb
		this.onRevoke.remove(this.onRevoke.size() - 1).run();
		breadcrumbPanel.remove(breadcrumbPanel.getWidgetCount() - 1);
		breadcrumbPanel.remove(breadcrumbPanel.getWidgetCount() - 1);
		this.onReturn.remove(this.onReturn.size() - 1);

		// Execute on return of the last crumb and also make it into current
		String text = ((Anchor) breadcrumbPanel.getWidget(
				breadcrumbPanel.getWidgetCount() - 1)).getText();
		breadcrumbPanel.remove(breadcrumbPanel.getWidgetCount() - 1);
		breadcrumbPanel.add(makeCurrentCrumb(text));
	}


	private Widget getGTSeparator() {
		return new InlineLabel(" > ");
	}

	private Widget makeCurrentCrumb(String text) {
		InlineLabel current = new InlineLabel(text);
		current.setStyleName("currentBread");
		return current;
	}

	private Widget makeCrumbLink(String text, final int stackIndex) {
		Anchor home = new Anchor(text);
		home.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {

				for (int i = 0; i < breadcrumbPanel.getWidgetCount(); i++) {
					if (breadcrumbPanel.getWidget(i) == event.getSource()) {
						// onRevoke until we see the same onReturn var instance passed
						while (onReturn.size() - 1 > (i / 2)) {
							BreadCrumbManager.this.popEntryNoRun();
						}
						onReturn.get(i / 2).run();
					}
				}
			}
		});
		return home;
	}



//	private Widget getMapPrecinctHome() {
//		Anchor mapPrecinct = new Anchor("Map Precinct");
//		mapPrecinct.addClickHandler(new ClickHandler() {
//			@Override
//			public void onClick(ClickEvent event) {
//				breadCrumbs.remove(breadCrumbs.getWidgetCount() - 1);
//				breadCrumbs.remove(breadCrumbs.getWidgetCount() - 1);
//				breadCrumbs.remove(breadCrumbs.getWidgetCount() - 1);
//				resetToMapPrecinctLanding();
//			}
//		});
//		return mapPrecinct;
//	}
//
//	private Widget getCityPage(String cityDisplayName) {
//		InlineLabel current = new InlineLabel(" " + cityDisplayName + " ");
//		current.setStyleName("currentBread");
//		return current;
//	}
//
//	private Widget getPrecinctSelectReturn(String cityDisplayName, final AddressMappingBLPanel panel) {
//		Anchor home = new Anchor(cityDisplayName);
//		home.addClickHandler(new ClickHandler() {
//			@Override
//			public void onClick(ClickEvent event) {
//				panel.resetMap();
//				resetToPrecinctList();
//			}
//
//		});
//		return home;
//	}

}
